使用String Array的自定义列表视图不起作用notifyDataSetChanged()

时间:2014-10-01 06:20:05

标签: android android-layout custom-lists notifydatasetchanged

我有一个带字符串数组的列表活动,当我点击列表项(convertview)时,我想编辑我的字符串。 一旦我编辑了一个字符串然后我按下了Guncelle Button我看到了新文本,每个想法都是oke。 但是当我刷新列表时,我看不到新文本。它没有变化。

我使用notifyDataSetChanged,但我认为它不起作用。我该如何解决这个问题。

public class huawei_list extends ListActivity {

private static int selected_item = -1;
private String[] phones = { "(0554) 741 xx xx", "(0554) 582 xx xx",
        "(0543) 613 xx xx", "(0543) 613 xx xx", "(0507) 183 xx xx",
        "(0216) 444 xx xx" };
private String[] birimler = { "HUAWEI NOBET", "NAZMİ İŞLER", "DENİZ NAMAL",
        "OKTAY TATAR", "ALP SİVRİ", "HUAWEI ACİL DESTEK" };
private telefonlarAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // birimler = getResources().getStringArray(R.array.birimler_huawei);

    mAdapter = new telefonlarAdapter();
    setListAdapter(mAdapter);
}

private static class AccessoriesViewHolder {
    public ListView list;
    public ImageView list_image;
    public TextView textview_list;
    public TextView textview_list2;
}

private class telefonlarAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return birimler.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return birimler[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        AccessoriesViewHolder holder = null;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.single_row,
                    parent, false);
            holder = new AccessoriesViewHolder();
            holder.list_image = (ImageView) convertView.findViewById(R.id.list_image);
            holder.textview_list = (TextView) convertView.findViewById(R.id.textview_list);
            holder.textview_list2 = (TextView) convertView.findViewById(R.id.textview_list2);
            holder.list = (ListView) convertView.findViewById(R.layout.single_row);
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    // Toast.makeText(getApplicationContext(),
                    // "Bu Tıkladığınız "+position+". satırdır" ,
                    // 1000).show();
                    selected_item = position;
                    final Dialog dialog_guncelle = new Dialog(
                            huawei_list.this);
                    dialog_guncelle.setContentView(R.layout.dialog);
                    dialog_guncelle.setCancelable(true);
                    dialog_guncelle.setTitle(birimler[selected_item]);
                    dialog_guncelle.show();
                    ImageView dialogresim = (ImageView) dialog_guncelle.findViewById(R.id.imageView1);
                    TextView guncelleText = (TextView) dialog_guncelle.findViewById(R.id.guncelle_text);
                    final EditText editIsim = (EditText) dialog_guncelle.findViewById(R.id.editText_isim);
                    final EditText editTelefone = (EditText) dialog_guncelle.findViewById(R.id.editText_telefone);
                    Button guncelle = (Button) dialog_guncelle.findViewById(R.id.guncelle);
                    Button iptalButon = (Button) dialog_guncelle.findViewById(R.id.iptal);
                    guncelleText.setText(birimler[selected_item]);
                    editIsim.setText(birimler[selected_item]);
                    editTelefone.setText(phones[selected_item]);
                    iptalButon.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            dialog_guncelle.cancel();
                        }
                    });
                    guncelle.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            birimler[selected_item] = editIsim.getText().toString();
                            phones[selected_item] = editTelefone.getText().toString();
                            notifyDataSetChanged();
                            dialog_guncelle.dismiss();

                        }
                    });

                }
            });

            ((Button) convertView.findViewById(R.id.call_button))
                    .setOnClickListener(call_buttonClisckListener);
            convertView.setTag(holder);
        } else {
            holder = (AccessoriesViewHolder) convertView.getTag();
        }

        String s = phones[position];
        holder.textview_list.setText(phones[position]);
        holder.textview_list2.setText(birimler[position]);

        if (s.contains("(0216)")) {
            holder.list_image.setImageResource(R.drawable.masaustuphone);
        } else {
            holder.list_image.setImageResource(R.drawable.gsm);
        }

        return convertView;

    }
}

private OnClickListener call_buttonClisckListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Cyril: Not implemented yet!

        final int position = getListView().getPositionForView(v);
        if (position != ListView.INVALID_POSITION) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phones[position]));
            startActivity(intent);
            // Toast.makeText(getApplicationContext(),
            // ""+getResources().getStringArray(R.array.huawei_rsmc)[position],
            // Toast.LENGTH_SHORT).show();
        }

    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;

}

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    if (item.getItemId() == R.id.aboutmenu)
        ;
    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.popup);
    dialog.setTitle("İLETİŞİM");
    dialog.setCancelable(true);
    dialog.show();
    TextView hakkindamail = (TextView) dialog
            .findViewById(R.id.hakkindamail);
    TextView hakkindamobile = (TextView) dialog
            .findViewById(R.id.hakkindamobile);

    hakkindamobile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:0507771xxxx"));
            startActivity(intent);

        }
    });
    hakkindamail.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            String[] TO = { "mustafa.kayahan@xxxxx.com" };
            Intent intentmail = new Intent(Intent.ACTION_SEND);
            intentmail.setData(Uri.parse("mailto:"));
            intentmail.putExtra(Intent.EXTRA_EMAIL, TO);
            intentmail.putExtra(intentmail.EXTRA_SUBJECT,
                    "SMC REHBER HAKKINDA");
            intentmail.setType("text/plain");
            try {
                startActivity(Intent.createChooser(intentmail,
                        "Send mail..."));
                finish();

            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(huawei_list.this,
                        "There is no email client installed.",
                        Toast.LENGTH_SHORT).show();
            }

        }
    });

    return super.onOptionsItemSelected(item);

}

}

这是我的自定义布局

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="horizontal" >

<ImageView
    android:id="@+id/list_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_launcher" />

  <Button
      android:id="@+id/call_button"
      android:layout_width="72dp"
      android:layout_height="72dp"
      android:layout_alignBottom="@+id/list_image"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:background="@drawable/selector"
      android:focusable="false"
      android:shadowColor="#A8A8A8"
      android:shadowDx="0"
      android:shadowDy="0"
      android:shadowRadius="0"
      android:text="Ara"
      android:textColor="#ffffff"
      android:textSize="30sp" />

  <TextView
      android:id="@+id/textview_list2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_toRightOf="@+id/list_image"
      android:text=""
      android:textStyle="bold" 
      android:textColor="@color/birimler"/>

  <TextView
      android:id="@+id/textview_list"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/list_image"
      android:layout_alignLeft="@+id/textview_list2"
      android:text=""
      android:textAppearance="?android:attr/textAppearanceSmall" />

  <TextView
      android:id="@+id/textview_list3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textview_list2"
      android:layout_below="@+id/textview_list2"
      android:text=""
      android:textSize="8sp"/>

  </RelativeLayout>

对话框布局

    <?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"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/userinfoicon" />

    <TextView
        android:id="@+id/guncelle_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:text="Large Text"
        android:textSize="25dp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="56dp"
        android:layout_height="wrap_content"
        android:text="İsim"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText_isim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="56dp"
        android:layout_height="wrap_content"
        android:text="Tel"
        android:textAlignment="center"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText_telefone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.86"
        android:inputType="textNoSuggestions" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/guncelle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:background="@drawable/selector"
        android:text="Güncelle"
        android:textColor="#FFFF" />

    <Button
        android:id="@+id/iptal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:background="@drawable/selector"
        android:text="İptal"
        android:textColor="#FFFF" />
 </LinearLayout>

 </LinearLayout>

1 个答案:

答案 0 :(得分:0)

在向StringArray添加新数据后,再调用您的适配器并进一步绑定数据。 替换

notifyDataSetChanged();

mAdapter = new telefonlarAdapter();
setListAdapter(mAdapter);

如果是setListAdapter(mAdapter);在你的Adapter类中工作的方法只是设置listview的id并使用viewbyid获取id然后将适配器设置为它。

多数民众赞成......