将新项插入MySQL后刷新ListView

时间:2012-06-24 15:39:20

标签: android mysql listview refresh

我尝试了很多方法和建议,但我的代码不正确。这就是我所做的。需要添加什么代码才能在添加新项目后,项目显示在列表中?

以下是上下文:

在操作栏中,有一个“添加项目”按钮。对话框打开,在对话框的onclick中,它将您在单个字段中键入的单词插入数据库(MySQL),解除对话框,然后......这就是我的麻烦所在......

编辑:我试过了 - yourAdapter.notifyDataSetChanged(); 我根本不知道在哪里使用它或如何使用它,因为它不会改变任何东西。

 public class Items extends ListActivity {


List<String> items = null;
String NewItem;
ArrayAdapter<String> adapter;
ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    items = new ArrayList<String>();

    new task().execute();

}

 // Class that sets up ListView and populates from database on initial Activity Load
class task extends AsyncTask<String, String, Void> {

    @Override
    protected Void doInBackground(String... params) {


        // HttpPost ... etc
                    // BufferRedReader .. etc 


    }

    protected void onPostExecute(Void v) {

        String item;
        try {
            // JSON Stuff
            }
        } catch (JSONException e1) {


 //  Here is where ListView is set up when Activity/page first starts

        listView = getListView();
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long id) {

                Intent i = new Intent(getApplicationContext(), Rate.class);
                i.putExtra("name", items.get(arg2));
                i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(i);

            }
        });

        adapter = new ArrayAdapter<String>(Items.this, R.layout.list, items);
        setListAdapter(adapter);


    }
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.Add_Item:

        final Dialog dialog = new Dialog(Items.this);
        dialog.setContentView(R.layout.insert_dialog);
        dialog.setTitle("Insert Item");
        dialog.setCancelable(true);
        dialog.show();

        final EditText etInsert = (EditText) dialog
                .findViewById(R.id.etInsertItem);

        Button bInsert = (Button) dialog.findViewById(R.id.bInsert);
        bInsert.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                NewItem = etInsert.getText().toString();
                new insertTask().execute();


                dialog.dismiss();

            }
        });

        return true;
    case R.id.About:

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}


 // This Class is the "inserting item" task inside the dialog

class insertTask extends AsyncTask<String, String, Void> {

    @Override
    protected Void doInBackground(String... params) {


        try {
            // HttpPost Stuff ... running insert Script to Database here

        } 

    }

    protected void onPostExecute(Void v) {

            // Add ListView Refresh Here????

    }


}
 }

3 个答案:

答案 0 :(得分:3)

更新数据后直接使用adapter.notifyDataSetChanged();。那应该够了。更新模型数据后,您还可以在适配器本身中放置notifyDataSetChanged();

顺便说一下。我没有在您的代码中看到列表更新。当然,在调用notifyDataSetChanged();之前,您必须更新列表的模型数据。

答案 1 :(得分:1)

您可以尝试使用

yourAdapter.notifyDataSetChanged();

onPostExecute方法中。

答案 2 :(得分:1)

适配器中使用list<String> items来获取ListItems的数据。您需要将新项目添加到此列表,即items,然后在adapter.notifyDataSetChanged(); asyncTask的onPostExecute()中调用insertTask