动态CustomListView未获得更新

时间:2015-07-18 19:11:27

标签: android android-listview android-custom-view custom-lists

我使用customadapter实现了自定义列表视图。当我按下刷新按钮时,数据从数据库中获取并在列表视图中更新。但在我的情况下,项目会在前面的项目之后附加,即如果我在数据库中有2个项目而我按下刷新按钮而不更改数据库项目,则会追加相同的项目并显示4个列表项目。需要快速帮助。 notifyDatasetChanged()用于代码中。但我不知道它是否正确。

以下是MainActivity.java的代码

public class MainActivity extends Activity {

ListView lv;

TextView tv1,tv2,tv3;
ArrayList<String> a=new ArrayList<String>();
ArrayList<String> b=new ArrayList<String>();
ArrayList<String> c=new ArrayList<String>();
ArrayList<String> d=new ArrayList<String>();
String mydata,name,name1,society,date,venue;
public String[] s1 = new String[50];
public String[] s2=new String[50];
public String[] s3=new String[50];
public String[] s4=new String[50];
public int[] img = {R.drawable.rty, R.drawable.sf, R.drawable.rty};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1=(TextView)findViewById(R.id.textView);
    lv = (ListView) findViewById(R.id.listView);
    ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
    Boolean isInternetPresent = cd.isConnectingToInternet(); // true or false

    if(isInternetPresent) {
        new MyData().execute();
    }
    else
    Toast.makeText(MainActivity.this,"No Internet Connection",Toast.LENGTH_SHORT).show();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent in = new Intent(MainActivity.this, listclick.class);
            in.putExtra("position", position);
            startActivity(in);

        }
    });
}
public void abc(View v)
{
    Intent in=new Intent(MainActivity.this,webform.class);
    startActivity(in);

}
public void ref(View v)
{
    ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
    Boolean isInternetPresent = cd.isConnectingToInternet();
    if(isInternetPresent) {



        new MyData().execute();

    }
    else
        Toast.makeText(MainActivity.this,"No Internet Connection",Toast.LENGTH_SHORT).show();

}
public class MyData extends AsyncTask<String,String,String>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        CustomAdapter cad = new CustomAdapter(MainActivity.this, s1, img,s2,s3,s4);
        lv.setAdapter(cad);
        cad.notifyDataSetChanged();
    }

    @Override
    protected String doInBackground(String... params) {
        getData();
        return null;
    }
}

public void getData()
{
    try {
        HttpClient httpClient=new DefaultHttpClient();

        HttpPost httpPost=new HttpPost("http://collegeevents.esy.es/abc.php");
        HttpResponse response=httpClient.execute(httpPost);
        HttpEntity httpEntity=response.getEntity();
        InputStream is=httpEntity.getContent();
        BufferedReader reader=new BufferedReader(new InputStreamReader(is,"utf-8"),8);
        StringBuilder strbuilder=new StringBuilder();
        String line=null;
        while ((line=reader.readLine())!=null)
        {
            strbuilder.append(line);
        }
        is.close();
        mydata=strbuilder.toString();
        JSONArray obj=new JSONArray(mydata);
        for(int i=0;i<obj.length();i++)
        {

            JSONObject obj1=obj.getJSONObject(i);
            a.add(i,obj1.getString("Name"));
            b.add(i,obj1.getString("society"));
            c.add(i,obj1.getString("venue"));
            d.add(i,obj1.getString("date"));
        }
        String[] s = new String[a.size()];
        s=a.toArray(s);
        s1 = s;

        String[] soc = new String[b.size()];
        soc=b.toArray(soc);
        s2 = soc;

        String[] ven = new String[c.size()];
        ven=c.toArray(ven);
        s3 = ven;

        String[] dat = new String[d.size()];
        dat=d.toArray(dat);
        s4 = dat;
    }
    catch (Exception e)
    {

    }


}


}

这是CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<String> {

Context c1;
String s1[],soc[],ven[],dat[];
int s2[];
CustomAdapter(Context c,String s[],int s3[],String society[],String venue[],String date[])
{
    super(c, R.layout.listcustom, s);
    this.c1=c;
    this.s1=s;
    this.s2=s3;
    this.soc=society;
    this.ven=venue;
    this.dat=date;
}


@Override
public View getView(int position, View v, ViewGroup parent) {
    LayoutInflater li=(LayoutInflater) c1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v=li.inflate(R.layout.listcustom,parent,false);
    TextView tv=(TextView)v.findViewById(R.id.textView);
    TextView tv1=(TextView)v.findViewById(R.id.society);
    TextView tv2=(TextView)v.findViewById(R.id.venue);
    TextView tv3=(TextView)v.findViewById(R.id.date);
    tv.setText(s1[position]);
    tv1.setText(soc[position]);
    tv2.setText(ven[position]);
    tv3.setText(dat[position]);

    if(position%2==0) {
        tv.setTextColor(Color.parseColor("#01579B"));
        tv3.setTextColor(Color.parseColor("#01579B"));

    }
    else{
        tv.setTextColor(Color.parseColor("#00897B"));
        tv3.setTextColor(Color.parseColor("#00897B"));

    }

    v.setTag(position);
    //notifyDataSetChanged();
    return v;
}
}

1 个答案:

答案 0 :(得分:1)

我终于明白了。您的简短回答是问题 - 您尚未清除default: calc calc.o: calc.c calc.h gcc -Wall -std=c99 -o calc calc.c -lm clean: rm calc.o cleanall: clean rm calc abc d。因此,每个ArrayList调用都会添加数据,而之前的数据仍然存在。因此,您应该在getData()的开头添加a.clear()b.clear() ...等。

但我建议做出以下改进:

  1. 介绍适配器数据的实体。该实体将包含4个字段:名称,社团,地点,日期。 因此,您不需要使用4个数组和4个ArrayLists。为了方便我将使用“事件”作为实体名称。这将会像:
  2. 的AsyncTask:

    getData

    的活动:

    public class MyData extends AsyncTask<String, String, Event[]> {
        @Override
        protected Event[] doInBackground(String... params) {
            return getData();            
        }
        @Override
        protected void onPostExecute(Event[] s) {
            cad.clear();
            cad.addAll(s)
        }
    }
    

    CustomAdapter:

    public class MainActivity extends Activity {
        private CustomAdapter cad;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
            lv = (ListView) findViewById(R.id.listView);
            cad = new CustomAdapter(getApplicationContext());
        }
    }
    
    1. 请勿使用public class CustomAdapter extends ArrayAdapter<Event> { private final LayoutInflater inflater; public CustomAdapter(Context c, Event ev[]) { super(context, ev[]); inflate = LayoutInflater.from(context); } CustomAdapter(Context c) { this(context, new Event[0]); } @Override public View getView(int position, View convertView, ViewGroup parent) { final View v; if(convertView == null) { v = inflater.inflate(...) } else { v = convertView; } // find views or event better — use ViewHolder pattern Event evt = getItem(position); tv.setText(evt.getName()); ... return v; } } 标记。请改用view#setOnClickListener。 onClick绑定布局和活动实施,但这并不好。

    2. 使用明确的名称。像onClick而不是LoadDataTask