在列表视图中,我的列表包含四个数据,即Fine_ID,Street,Area和Car_No。当我点击Clamp Vehicle这是我的上下文菜单时,我想获取所有这些值。我想将所有这四个值传递到我的数据库中,但问题是,当我点击另一个列表时,它的剂量会改变之前的所有值。
这是我的钳制器活动代码
public class ClamperActivity extends ListActivity {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String CAR_NO = "car_no";
private static final String FINE_ID = "fine_id";
private static final String AREA = "area";
private static final String STREET = "street";
JSONArray jsonArray = null;
private static String userId = LoginActivity.user_id;
private static String fineId1;
private static String street1;
private static String area1;
private static String carNo1;
ArrayList<HashMap<String, String>> jsonlist;
public static String user_id="";
static String url= "http://192.168.1.7/testkrcs/main/get_clamped";
static String url1="https://192.168.1.3/testkrcs/main/clamp_vehicle";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clamper);
jsonlist = new ArrayList<HashMap<String,String>>();
ListView lv = (ListView) findViewById(android.R.id.list);
registerForContextMenu(lv);
new FetchVehiclesTask().execute();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Clamp Vehicle");
menu.add(0, v.getId(), 0, "Cancel");
}
@Override
public boolean onContextItemSelected(MenuItem item){
if(item.getTitle()=="Clamp Vehicle"){
carNo1 = ((TextView)findViewById(R.id.carNo))
.getText().toString();
fineId1 = ((TextView) findViewById(R.id.fineId))
.getText().toString();
area1 = ((TextView) findViewById(R.id.area1))
.getText().toString();
street1 = ((TextView)findViewById(R.id.street1))
.getText().toString();
//Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();
new ClamperVehicleTask().execute();
}
if(item.getTitle()=="Cancel"){
}else{
return false;
}
return true;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
openContextMenu(v);
}
class ClamperVehicleTask extends AsyncTask<String, String, String>
{
protected String doInBackground(String... args )
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("street",ClamperActivity.street1));
params.add(new BasicNameValuePair("area",ClamperActivity.area1));
params.add(new BasicNameValuePair("user_id",ClamperActivity.userId));
params.add(new BasicNameValuePair("fine_id",ClamperActivity.fineId1));
params.add(new BasicNameValuePair("car_no",ClamperActivity.carNo1));
JSONObject json = jsonParser.getJSONFromUrl(url1, params);
System.out.println("ClamperVehicleTask UID = "+ClamperActivity.userId);
System.out.println("ClamperVehicleTask Street = "+ClamperActivity.street1);
System.out.println("ClamperVehicleTask Area = "+ClamperActivity.area1);
System.out.println("ClamperVehicleTask Fine ID = "+ClamperActivity.fineId1);
System.out.println("ClamperVehicleTask Car No. = "+ClamperActivity.carNo1);
Log.d("ClamperVehicleTask Response", json.toString());
return null;
}
}
class FetchVehiclesTask extends AsyncTask<Void, Void, Void>
{
private ListActivity activity;
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ClamperActivity.this);
pDialog.setMessage("Fetching Vehicles List...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected Void doInBackground(Void... args0)
{
System.out.println("Login Street clamper ="+LoginActivity.street);
System.out.println("Login Area clamper ="+LoginActivity.area);
System.out.println("Login Uid clamper = "+LoginActivity.user_id);
JSONParser jsonParser = new JSONParser();
UserFunctions userFunction = new UserFunctions();
String json = userFunction.clamperFetch(LoginActivity.street, LoginActivity.area, LoginActivity.user_id);
Log.d("Response:",json);
if(json!=null)
{
try{
jsonArray = new JSONArray(json);
for(int i=0;i<jsonArray.length();i++)
{
JSONObject c = jsonArray.getJSONObject(i);
String fineId = c.getString(FINE_ID);
String street2 = c.getString(STREET);
String area2 = c.getString(AREA);
String carNo = c.getString(CAR_NO);
System.out.println("FineId = " +fineId);
System.out.println("Street = "+street2);
System.out.println("Area = "+area2);
System.out.println("Car_No = "+carNo);
HashMap<String,String>jsonArray = new HashMap<String,String>();
jsonArray.put(FINE_ID,fineId);
jsonArray.put(STREET, street2);
jsonArray.put(AREA, area2);
jsonArray.put(CAR_NO, carNo);
jsonlist.add(jsonArray);
}
}catch(JSONException e){
e.printStackTrace();
}
}else {
Log.e("JASON Parser", "Couldn't get any data from the url");
}
System.out.println("Fetching Response" +json);
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// dismiss the dialog once done
if(pDialog.isShowing())
{
pDialog.dismiss();
}
System.out.println("Result"+result);
ListAdapter adapter = new SimpleAdapter(
ClamperActivity.this,jsonlist,
R.layout.list_item,
new String[]{FINE_ID,STREET,AREA,CAR_NO},
new int[]{R.id.carNo,R.id.fineId,R.id.area1,R.id.street1});
setListAdapter(adapter);
System.out.println("Adapter"+adapter);
}
}
}
像这样我试图从列表视图中获取值,当我试图点击另一个列表时它不能正常工作它保持相同的值它没有改变新值,
public boolean onContextItemSelected(MenuItem item){
if(item.getTitle()=="Clamp Vehicle"){
carNo1 = ((TextView)findViewById(R.id.carNo))
.getText().toString();
fineId1 = ((TextView) findViewById(R.id.fineId))
.getText().toString();
area1 = ((TextView) findViewById(R.id.area1))
.getText().toString();
street1 = ((TextView)findViewById(R.id.street1))
.getText().toString();
//Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();
new ClamperVehicleTask().execute();
}
if(item.getTitle()=="Cancel"){
}else{
return false;
}
return true;
}
通过这种方式,我试图将所有四个值传递到数据库中 class ClamperVehicleTask扩展了AsyncTask
{
protected String doInBackground(String... args )
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("street",ClamperActivity.street1));
params.add(new BasicNameValuePair("area",ClamperActivity.area1));
params.add(new BasicNameValuePair("user_id",ClamperActivity.userId));
params.add(new BasicNameValuePair("fine_id",ClamperActivity.fineId1));
params.add(new BasicNameValuePair("car_no",ClamperActivity.carNo1));
JSONObject json = jsonParser.getJSONFromUrl(url1, params);
System.out.println("ClamperVehicleTask UID = "+ClamperActivity.userId);
System.out.println("ClamperVehicleTask Street = "+ClamperActivity.street1);
System.out.println("ClamperVehicleTask Area = "+ClamperActivity.area1);
System.out.println("ClamperVehicleTask Fine ID = "+ClamperActivity.fineId1);
System.out.println("ClamperVehicleTask Car No. = "+ClamperActivity.carNo1);
Log.d("ClamperVehicleTask Response", json.toString());
return null;
}
}
答案 0 :(得分:0)
在使用适配器类附加到listview之前,您必须已经存储了List。只需使用get(position)
。
例如: - 我使用简单的示例来解释概念。
我希望您知道如何在 arraylist 中添加数据。
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter(this,android.R.layout.simple_list.xml, list)
listview.setAdapter(adapter)
现在您已将该列表附加到适配器类。然后在onListItemClick(ListView l, View v, int position, long id) event
上包含以下代码
list.get(position)
答案 1 :(得分:0)
在onContextItemSelected
处理程序中,您可以使用AdapterContextMenuInfo
获取有关所选项目位置的信息,甚至是与列表项关联的视图。校验
How to get View in OnContextItemSelected event?。
在检索到的项目视图上调用findViewById
方法,您可以访问所单击项目的文本视图。
尝试利用AsyncTask参数将字符串传递给doInBackground()方法,而不是静态类字段。
@Override
public boolean onContextItemSelected(MenuItem item){
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
View view = info.targetView;
String carNo1, fineId1, area1, street1, uId = userId;
if(item.getTitle()=="Clamp Vehicle"){
carNo1 = ((TextView) view.findViewById(R.id.carNo))
.getText().toString();
fineId1 = ((TextView) view.findViewById(R.id.fineId))
.getText().toString();
area1 = ((TextView) view.findViewById(R.id.area1))
.getText().toString();
street1 = ((TextView) view.findViewById(R.id.street1))
.getText().toString();
new ClamperVehicleTask().execute(carNo1, fineId1, uId, area1, street1);
}
if(item.getTitle()=="Cancel"){
}else{
return false;
}
return true;
}
doInBackground()中的更改:
protected String doInBackground(String... args )
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("street",args[4]));
params.add(new BasicNameValuePair("area",args[3]));
params.add(new BasicNameValuePair("user_id",args[2]));
params.add(new BasicNameValuePair("fine_id",args[1]));
params.add(new BasicNameValuePair("car_no",args[0]));
JSONObject json = jsonParser.getJSONFromUrl(url1, params);
System.out.println("ClamperVehicleTask UID = "+args[2]);
System.out.println("ClamperVehicleTask Street = "+args[4]);
System.out.println("ClamperVehicleTask Area = "+args[3]);
System.out.println("ClamperVehicleTask Fine ID = "+args[1]);
System.out.println("ClamperVehicleTask Car No. = "+args[0]);
Log.d("ClamperVehicleTask Response", json.toString());
return null;
}
此外,我还建议您以更正确的方式打开上下文菜单(选中https://stackoverflow.com/a/8872337/3423110)
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
l.showContextMenuForChild(v);
}
答案 2 :(得分:0)
实际上你有一个问题,你在主要布局中全局查找你的视图,而它是你的listview的一部分所以找到这样的,这样你就可以得到这些值。 public boolean onContextItemSelected(MenuItem item){
if(item.getTitle()==&#34; Clamp Vehicle&#34;){
carNo1 =((TextView)listview.findViewById(R.id.carNo))
.getText()的toString();
fineId1 =((TextView)listview.findViewById(R.id.fineId))
.getText()的toString();
area1 =((TextView)listview.findViewById(R.id.area1))
.getText()的toString();
street1 =((TextView)listview.findViewById(R.id.street1))
.getText()的toString();
//Toast.makeText(getApplicationContext(),"调用代码&#34;,Toast.LENGTH_LONG)。show();
new ClamperVehicleTask()。execute();
}
if(item.getTitle()=="Cancel"){
}else{
return false;
}
return true;
}