Android studio .setOnItemClickListener,用于一个活动中的多个选项

时间:2017-03-02 23:06:47

标签: java listview android-studio

所以我有一个带溢出菜单的活动,让用户可以选择将数据显示在一个列表视图中的不同类别。我需要为listview设置一个clicklistener,它可以用于每个类别,以从所选类别中获取信息。目前我只能将其用于一个类别,但没有灵活性(因为如果我设置它,它将适用于总线)。我已经尝试了一些if if语句在特定类别中注册但没有运气的情况。

public class AllAttractions extends AppCompatActivity implements View.OnClickListener  {

DBManager db;
ListView ls;

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

// JSON Node names for attraction
private static final String TAG_SUCCESS = "success";
private static final String TAG_ATTRACTION = "attraction";
private static final String TAG_ATTRACTIONID = "Id";
private static final String TAG_NAME = "Name";
private static final String TAG_TYPE = "Type";
private static final String TAG_LOCATION = "Location";
private static final String TAG_OPENING = "OpeningTime";
private static final String TAG_CLOSING = "ClosingTime";
private static final String TAG_NEARBYSTOP = "NearbyStop";
private static final String TAG_LATITUDE = "Latitude";
private static final String TAG_LONGITUDE = "Longitude";

//JSON Node names for transport
private static final String TAG_TRANSPORT = "transport";
private static final String TAG_TRANSPORTID = "Id";
private static final String TAG_TIME = "Time";
private static final String TAG_NEXTSTOP = "NextStop";
private static final String TAG_PHONENUMBER = "PhoneNumber";

private static final String TAG_BUS_STOPS = "bus_stops";
private static final String TAG_ID = "id";
private static final String TAG_STOP = "stop";
private static final String TAG_BUS_NAME = "bus_name";
private static final String TAG_NEXT_STOP = "next_stop";

// attraction JSONArray
JSONArray attraction = null;
JSONArray transport = null;
JSONArray bus_stops = null;
String pid;
String pid2;
String pid1;

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

    db = new DBManager(this);

    // Hashmap for ListView



    // Get listview
    ls = (ListView) findViewById(R.id.list_search);
    this.registerForContextMenu(ls);
    ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // getting values from selected ListItem
            //can only get one of these to work otherwise will get error but need to be able to get anyone from any of the 3 below
            String BMAttractionName = ((TextView)view.findViewById(R.id.name)).getText().toString();
            String BMTransportName = ((TextView)view.findViewById(R.id.TransportName)).getText().toString();
            String BMBusName = ((TextView)view.findViewById(R.id.busStop)).getText().toString();


        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.optionsmenu,menu);
    return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if(v.getId()== R.id.list_search){
        this.getMenuInflater().inflate(R.menu.context_menu_more,menu);
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.menuBookmark:

            System.out.println(pid);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);

    if(id == R.id.attractionSub1){
        new LoadAllCafe().execute();
    }else if(id == R.id.attractionSub2){
    }else if(id == R.id.attractionSub3){
    }else if(id == R.id.attractionSub4){
    }else if(id == R.id.attractionSub5){
    }else if(id == R.id.attractionSub6){
    }else if(id ==R.id.attractionSub7){
    }else if(id == R.id.attractionSub8){
    }else if(id == R.id.attractionSub9){
    } else if(id == R.id.transportSub1){
        new LoadAllTransport().execute();
    }else if(id == R.id.transportSub2){
        new LoadAllBus().execute();
    }
    return true;
}

@Override
public void onClick(View view) {

}

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all attraction
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllAttractions.this, cafeList,
                        R.layout.list_attraction, new String[]{TAG_ATTRACTIONID,
                        TAG_NAME,TAG_TYPE,TAG_LOCATION,TAG_OPENING,TAG_CLOSING,TAG_NEARBYSTOP,TAG_LATITUDE,TAG_LONGITUDE},
                        new int[]{R.id.Attractionid, R.id.name, R.id.type, R.id.location,R.id.open,R.id.close,R.id.nearbystop, R.id.tvLat,R.id.tvLon});
                // updating listview
                ls.setAdapter(adapter);
            }
        });

    }

}


    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all attraction
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllAttractions.this, transportList,
                        R.layout.list_transport, new String[]{TAG_TRANSPORTID,
                        TAG_NAME,TAG_LOCATION,TAG_PHONENUMBER},
                        new int[]{R.id.transportid, R.id.TransportName, R.id.TransportLocation,R.id.TransportPhoneNumber});
                // updating listview
                ls.setAdapter(adapter);
            }
        });

    }

}


    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all attraction
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllAttractions.this, busList,
                        R.layout.list_bus, new String[]{TAG_ID,
                        TAG_STOP,TAG_BUS_NAME,TAG_NEXT_STOP},
                        new int[]{R.id.busID, R.id.busStop, R.id.busName, R.id.busNextStop});
                // updating listview
                ls.setAdapter(adapter);
            }
        });

    }

}
}

ls.setOnItemClickListener中的主要部分位于顶部附近(新的AdapterView.OnItemClickListener(){....

这里我有3个字符串试图访问每个不同类别中的数据名称。但是只能让这个为一个工作(如果我评论2,另一个将工作)但我需要他们所有能够提取信息,如果我选择相关的类别。感谢能帮助我的人。

0 个答案:

没有答案