我已经实现了一个从json数据填充的多列列表视图。我也为它设置了一个onClickListener。但是当我点击listView时运行应用程序我只能获取列表的第一项。
我想要实现的是当我点击listView中的任何项目时我应该能够获得行项目。我使用的代码如下
Adapter.java
private Activity activity;
private static ArrayList prefrenceid,partnerid;
private static LayoutInflater inflater = null;
public CompanyRegistrationAdapter(Activity a, ArrayList pref, ArrayList part) {
activity = a;
this.prefrenceid = pref;
this.partnerid=part;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return prefrenceid.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.company_registation_adapter, null);
TextView preferences = (TextView) vi.findViewById(R.id.textView20);
String song = prefrenceid.get(position).toString();
preferences.setText(song);
TextView partner = (TextView) vi.findViewById(R.id.textView21);
String song2 = partnerid.get(position).toString();
partner.setText(song2);
return vi;
}
MainActivity.java
AsyncHttpClient usercheck = new AsyncHttpClient();
usercheck.get("REST URL", new AsyncHttpResponseHandler() {
public void onSuccess(String response) {
try {
progressDialog.hide();
JSONObject obj3 = new JSONObject(response);
final JSONArray jsonArray3 = obj3.getJSONArray("REST Method");
int length3 = jsonArray3.length();
if (length3 != 0) {
for (int i = 0; i < length3; i++) {
JSONObject user3 = jsonArray3.getJSONObject(i);
preference_array.add(user3.getString("GCP_NUMBER").toString());
partner_array.add(user3.getString("COMPANY_NAME").toString());
}
adapter = new CompanyRegistrationAdapter(CompanyRegistrationResult.this, preference_array, partner_array);
fav.setAdapter(adapter);
fav.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
String iPartnerID = jsonArray3.getJSONObject(position).getString("ID");
Toast.makeText(getApplicationContext(),iPartnerID,Toast.LENGTH_SHORT).show();
Intent compRegister=new Intent(getApplicationContext(),CompanyRegistration.class);
compRegister.putExtra("gcp", gcp);
compRegister.putExtra("compName", compName);
startActivity(compRegister);
} catch (JSONException e) {
e.printStackTrace();
}
String gcp=((TextView) findViewById(R.id.textView20)).getText().toString();
String compName=((TextView) findViewById(R.id.textView21)).getText().toString();
}
});
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}