استفاده از کد json در list view (یک مشکل عجیب!)
ُسلام دوستان
راستش من یه مشکل عجیب غریب دارم
لیست ویوی من درست کار میکنه. یعنی این کد رو درست اجرا میکنه و لیست ویو نمایش داده میشه:
نکته دیگه اینکه وقتی از سمت سرور اطلاعات رو میگیرم، باز کد json درست دریافت میشه
ولی وقتی میخام به لیست ویو بریزمش، چیزی نشون داده نمیشه. اینم کد :
یعنی log هارو درست نشون میده ولی چیزی داخل listview نشون نمیده. انگار listview رو نذاشتم اصلا. خالی خالی!
کد لیست ویو:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lstView"/> </LinearLayout> </LinearLayout>
کدهای آداپتورم هم تقریبا مثل کدهای استاد هست:
public class AdapterSoureh extends ArrayAdapter<StructSoureh> { public AdapterSoureh(ArrayList<StructSoureh> array) { super(G.context, R.layout.name_soureh_note, array); } private static class ViewHolder { public TextView txtNameSoureh; public TextView txtNumber; public Button btnOpen; public ViewHolder(View view) { txtNameSoureh = (TextView) view.findViewById(R.id.txtNameSoureh); txtNumber = (TextView) view.findViewById(R.id.txtNumber); btnOpen = (Button) view.findViewById(R.id.btnOpen); } public void Fill( ArrayAdapter<StructSoureh> adapter, StructSoureh item, final int position) { txtNameSoureh.setText(item.Name); txtNumber.setText(Integer.toString(item.NumberAyes)); btnOpen.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { //go to next intent Intent intent = new Intent(G.currentActivity, ActivitySelectSession.class); intent.putExtra("POSITION", position); G.currentActivity.startActivity(intent); } }); //barresi kardane boodane tamaame file haaye mp3 marboot be soureh morede nazar dar sd card if(item.Downloaded) { // image downloaded = true } else { //image downloaded = false } } } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; StructSoureh item = getItem(position); if(convertView == null) { convertView = G.Inflater.inflate(R.layout.name_soureh_note, parent, false); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.Fill(this, item, position); return convertView; } }
تشکر بابت کمکی که میکنید.
نه دوست عزیز. به جای wrap_content، مقادیر :
<ListView android:layout_width="380dp" android:layout_height="332dp" android:id="@+id/lstView"/>
زیر رو گذاشتم که بازم چیزی نشون نمیده.
اینم کدهای name_soureh_note:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/btnOpen" android:layout_marginLeft="0dp" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/txtNumber" android:text="number" android:layout_marginLeft="32dip" android:layout_weight=".33" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="نام سوره" android:id="@+id/txtNameSoureh" android:layout_marginLeft="32dip" android:layout_weight=".33" /> </LinearLayout>
تغییراتی که گفتید:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/btnOpen" android:layout_marginLeft="0dp" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/txtNumber" android:text="number" android:layout_marginLeft="32dip" android:layout_weight=".33" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="نام سوره" android:id="@+id/txtNameSoureh" android:layout_marginLeft="32dip" android:layout_weight=".33" /> </LinearLayout>
یه سوال:
کدهای ارتباط با سرور رو من داخل یک thread میذارم.
درسته؟ تا اجرا بشه و برنامه کرش نکنه.
یعنی thread ای که مربوط به ui برنامه میشه ، داره برای خودش کار میکنه و این thread هم برای خودش میره با سرور ارتباط برقرار میکنه. درسته؟
پس چطوری میتونه به ui دسترسی داشته باشه و مقادیر رو به listview نشون بده؟
حل شد!!
وقتی که یک سری کد رو داخل Thread می نویسید اگه توی کدها کدی باشه که UI برنامه رو تغییر بده (مثلا setText برای TextView) برنامه کرش می کنه چون که فقط باید از طریق Thread اصلی UI رو تغییر بدید. به خاطر همین توی Thread هرجا کدی می خواست که UI رو تغییر بده اون کد رو داخل Handler می نویسید. مثلا:
finalTextView txt =(TextView) findViewById(R.id.txt);
finalHandler handler =newHandler();
Thread thread =newThread(newRunnable(){
@Override
publicvoid run(){
handler.post(newRunnable(){
@Override
publicvoid run(){
txt.setText("سلام");
}
});
}
});
با این کد txt.setText که توی thread نوشته شده در واقع توی Thread اصلی اجرا میشه.
باید به این نکته توجه داشته باشی که Handler رو توی thread تعریف نکنی.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .